package com.neusoft.bizcore.auth.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotBlank;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractModel;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
public class AuthRole extends AbstractModel {

    private static final long serialVersionUID = 1L;

    /** 角色名 */
    @NotBlank(message = "角色名不能为空")
    @Length(max = 50, message = "角色名不能超过{max}位")
    @Column(length = 50, nullable = false)
    private String name;

    /** 角色编码 */
    @NotBlank(message = "角色编码不能为空")
    @Length(max = 255, message = "角色编码不能超过{max}位")
    @Column(length = 255, nullable = false, unique = true)
    private String code;

    /**
     * 关联用户
     */
    @ManyToMany(mappedBy = "roles")
    private List<AuthUser> users = new ArrayList<>();

    /**
     * 关联权限
     */
    @ManyToMany(targetEntity = AuthPermission.class)
    @JoinTable(name = "auth_ref_role_permissions")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<AuthPermission> permissions = new ArrayList<>();

}
